package ui {
import core.Facade;

import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.ui.Mouse;

import ru.cjay.loading.ResourceManager;

public class FlashMessagePopup {
    private static var _instance:FlashMessagePopup;
    private static var allowInstantiation:Boolean;

     public static function getInstance():FlashMessagePopup {
        if (_instance == null) {
            allowInstantiation = true;
            _instance = new FlashMessagePopup();
            allowInstantiation = false;
        }
        return _instance;
    }

    public function FlashMessagePopup() {
        if (!allowInstantiation) {
            throw new Error("Error: Instantiation failed: Use SingletonDemo.getInstance() instead of new.");
        }
    }

    public static function showFlashMessage(message:String):void{
        var popup:MovieClip = new (ResourceManager.getInstance().getSwfAssetClass("ui:FlashPopup") as Class )();

        Facade.getInstance().stage.addChild(popup);
        popup.x = (Facade.WIDTH - popup.width)/2;
        popup.y =(Facade.HEIGHT - popup.height)/2;
        popup.message.text = message;

        popup.btn_close.addEventListener(MouseEvent.CLICK, function(event:MouseEvent){
            var popup = (event.currentTarget as SimpleButton);
            if (Facade.getInstance().stage.contains(popup.parent)){
                Facade.getInstance().stage.removeChild(popup.parent);
            }
        })

    }
}
}
